package be.kuleuven.peno3.mobiletoledo.Data.Client;

import java.util.Calendar;

import android.content.Context;
import android.net.ConnectivityManager;
import be.kuleuven.peno3.mobiletoledo.Data.LocalDAO;
import be.kuleuven.peno3.mobiletoledo.model.CourseActivity;
import be.kuleuven.peno3.mobiletoledo.model.Schedule;

import com.google.gson.Gson;

public abstract class ScheduleClient extends Client{
	private static CourseActivity[] getCourseActivities(Calendar begin, Calendar end, int course_id, String group, String study_programme, Context ctx){
		String courseActivitiesID = "CourseActivities_" + toSQLString(begin) + "_" + toSQLString(end) + "_" + course_id + "_" + group + "_" + study_programme;
		System.out.println(courseActivitiesID);
		LocalDAO db = new LocalDAO(ctx);
		if(isOnline(ctx)) {
			try {
				String beginDate=toSQLString(begin);
				String endDate=toSQLString(end);
				String url ="beginDate="+ beginDate +"&endDate="+endDate+"&group="+group+"&course="+course_id+"&study_programme="+study_programme;
				url=encode(url);
				url=host+"/ScheduleHandler/getCourseActivities?"+url;
				String json = stringOfUrl(url);
				CourseActivity[] courseActivities =new Gson().fromJson(json.toString(), CourseActivity[].class);
				if(courseActivities.length == 0 || courseActivities == null){
					return null;
				}
				db.open();
				if(db.fetchProperty(courseActivitiesID) == null){
					boolean success = db.createEntry(courseActivitiesID, new Gson().toJson(courseActivities));
					System.out.println("create " + success);
				}
				else{
					boolean success=db.updateEntry(courseActivitiesID, new Gson().toJson(courseActivities));
					System.out.println("update " + success);
				}
				db.close();
				return courseActivities;
			} catch (Exception e) {
				db.open();
				String temp = db.fetchProperty(courseActivitiesID);
				db.close();
				try{
					CourseActivity[] courseActivities =new Gson().fromJson(temp.toString(), CourseActivity[].class);
					return courseActivities;
				}
				catch(Exception ee){
					ee.printStackTrace();
					return null;
				}
			}
		}
		else { 
			db.open();
			String temp = db.fetchProperty(courseActivitiesID);
			db.close();
			try{
				CourseActivity[] courseActivities =new Gson().fromJson(temp.toString(), CourseActivity[].class);
				return courseActivities;
			}
			catch(Exception ee){
				ee.printStackTrace();
				return null;
			}
		}
	}
	
	private static boolean isOnline(Context ctx) {
		ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
		try {
			return cm.getActiveNetworkInfo().isConnected();
		} catch(NullPointerException e) {
			return false;
		}
	}
    
    public static Schedule getSchedule(Calendar begin,Calendar end,int course_id,String group,String study_programme, Context ctx){
		Schedule sched = new Schedule();
		sched.setCourseActivities(getCourseActivities(begin, end, course_id, group, study_programme, ctx));
		return sched;	
    }
   
    
}
